<?php
/**
 * 微信投票奖品兑换码 Controller
 * 
 * @path Apps/Mp/Controller/WxEventShakeAwardSnController.class.php
 * @author Zix <zix2002@gmail.com> 
 * @version v1.0 , 2015-09-11
 */

namespace Mp\Controller;

use Common\Service\WxEventShakeAwardService;
use Common\Service\WxEventShakeAwardSnService;

class WxEventShakeAwardSnController extends MpController {

  public function index( $award_id ) {
    $action = I('get.action', 'read');
    $data = $this->_init_data();
    
    $WxEventShakeAward = WxEventShakeAwardService::instance();
    $data['award_data'] = $WxEventShakeAward->get_by_id($award_id);
    
    $data['module_name'] = $data['award_data']['name'] . ' 的 奖品兑换码';
    $data['module_url'] = U( $this->base_path . 'index');

    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id'    => 'menu_Mp_MerWxSetting_index' ,
      'award_id'   => $award_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ) . "/$award_id",
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'upload_excel_url' => U($this->base_path . 'upload') . '/tmp',
      'create_by_excel_url' => U($this->base_path . 'create_by_excel') . '/' . $award_id,
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $WxEventShakeAwardSn->get_default_row() 
    );

    $data['param'] = array(
      'status' => $WxEventShakeAwardSn->status 
    );

    $this->display_with_layout($data, $this->controller .':index');
  }

  function read( $award_id ) {
    $post = I('post.');

    $config = array(
      'award_id' => $award_id ,
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'award_id' => $award_id ,
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    $data['rows'] = $WxEventShakeAwardSn->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $WxEventShakeAwardSn->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    $ret = $WxEventShakeAwardSn->create($post);
    $this->ajaxReturn($ret);
  }
  
  function create_by_excel($award_id) {
    $filename = I('post.filename');

    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    
    $ret = $WxEventShakeAwardSn->create_by_excel($award_id , $filename );
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    $ret = $WxEventShakeAwardSn->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventShakeAwardSn = WxEventShakeAwardSnService::instance();
    $ret = $WxEventShakeAwardSn->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
